package com.ns002.yigou.quote.json;

import com.alibaba.fastjson.JSONObject;
import com.ns002.yigou.quote.exception.IllegalSignException;
import com.ns002.yigou.quote.exception.LogicException;
import com.ns002.yigou.quote.globals.GlobalsConst;
import com.opslab.util.DateStyle;
import com.opslab.util.DateUtil;
import com.opslab.util.MD5Util;
import com.opslab.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.login.LoginException;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by xu on 2016/12/15.
 * 用于解析客户端传递的参数
 * <p>
 * {"longitude":"123.12635454","latitude":"32.2668555","appVersion":"10","dip":"320","os":"ANDROID","channelId":"应用宝","data":{"phone":"18666626405"}}
 * http://localhost:8080/app/component/sendRegMsg?parameter={%22longitude%22:%22123.12635454%22,%22latitude%22:%2232.2668555%22,%22appVersion%22:%2210%22,%22dip%22:%22320%22,%22os%22:%22ANDROID%22,%22channelId%22:%22%E5%BA%94%E7%94%A8%E5%AE%9D%22,%22data%22:{%22phone%22:%2218666626405%22}}
 */
public class JSONParameter {

    private static final Logger log = LoggerFactory.getLogger(JSONParameter.class);

    public static final String OS_ANDROID = "ANDROID";
    public static final String OS_IOS = "IOS";

//    /** 经度 */
//    private String longitude;
//    /** 纬度 */
//    private String latitude;
//    /** app版本 */
//    private String appVersion;
//    /** 屏幕分辨率 */
//    private String dip;
//    /** 系统类型 */
//    private String os;
//    /** 渠道 */
//    private String channelId;

//    "imei":"sdjfskfljdklfsjfsdjreklfjsdmer,.gd",  //用户机型唯一标识
//            "mac":"sdgdjusakldfjlksadfsdf",       //网卡标识
//            "ip":"192.168.1.100",     //内网ip
//            "lon":"",                         //经度
//            "lat":"",                           //纬度
//            "location":"广东省广州市番禺区南村镇保利大都汇",  //反地理编码
//            "os":"ANDROID",            //用户手机系统  ANDROID:安卓        IOS:苹果
//            "version":"10",                 //版本号
//            "channel":"Wandoujia"  //渠道

    /**
     * 主数据
     */
    private Map<String, Object> data;
    private String token;
    private String sign;
    private String imei;
    private String uuid;
    private String mac;
    private String ip;
    private String lon;
    private String lat;
    private String location;
    private String os;
    private String channel;
    private String version;
    private String cid;


    /**
     * 取值String
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        Object value = getData().get(key);
        if (value == null) {
            throw new LogicException(key + "不能为空");
        }
        return value.toString();
    }

    public String getString(String key, String defaultValue) {
        Object value = getData().get(key);
        if (value == null) {
            return defaultValue;
        }
        return value.toString();
    }

    /**
     * 取值int
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        Object value = getData().get(key);
        if (value == null || StringUtil.isEmpty(value.toString())) {
            throw new LogicException(key + "不能为空");
        }
        return Integer.parseInt(value.toString());
    }

    /**
     * 取值int
     *
     * @param key
     * @return
     */
    public int getInt(String key, int defaultVal) {
        Object value = getData().get(key);
        if (value == null) {
            return defaultVal;
        }
        return Integer.parseInt(value.toString());
    }

    public double getDouble(String key, double defaultVal) {
        Object value = getData().get(key);
        if (value == null) {
            return defaultVal;
        }
        return Double.parseDouble(value.toString());
    }

    public double getDouble(String key) {
        Object value = getData().get(key);
        if (value == null || StringUtil.isEmpty(value.toString())) {
            throw new LogicException(key + "不能为空");
        }
        return Double.parseDouble(value.toString());
    }


    public void setData(Map<String, Object> data) {
        this.data = data;
    }


    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public boolean isIOS() {
        if (!StringUtil.isEmpty(os)) {
            if (os.toUpperCase().equals(OS_IOS.toUpperCase())) {
                return true;
            }
        }

        return false;
    }

    public boolean isANDROID() {
        if (!StringUtil.isEmpty(os)) {
            if (os.toUpperCase().equals(OS_ANDROID.toUpperCase())) {
                return true;
            }
        }

        return false;
    }

    /**
     * 取得参数
     *
     * @param jsonParameter
     * @return
     * @throws IllegalSignException
     */
    public static final JSONParameter parser(String jsonParameter) throws IllegalSignException {
        if(StringUtil.isEmpty(jsonParameter)) throw new LogicException("请传递正确的参数");
        JSONParameter parameter = JSONObject.parseObject(jsonParameter, JSONParameter.class);


        TreeMap treeMap = new TreeMap();
        treeMap.putAll(parameter.getData());

        String sign = MD5Util.encode(JSONObject.toJSONString(treeMap) + GlobalsConst.APP_SECRET);
        if (sign.equals(parameter.getSign()) || "admin".equals(parameter.getSign())) {

            return parameter;
        } else {
            throw new IllegalSignException();
        }
    }

    static final int PAGE_SIZE = 12;

    /**
     * 获取分页索引， 前端页码从1开始，二后台是从0开始，所以减1
     */
    public int getPageIndex() {
        int pageIndex = getInt("pageIndex") - 1;
        return pageIndex < 0 ? 0 : pageIndex;
    }

    /**
     * 获取分页最大显示条数
     */
    public int getPageSize() {
        return getInt("pageSize", PAGE_SIZE);
    }


    public Map<String, Object> getData() {
        return data;
    }

    public String getImei() {

        if (StringUtil.isEmpty(imei)) {
            return uuid;
        }

        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getUuid() {

        if (StringUtil.isEmpty(uuid)) {
            return imei;
        }

        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}
